const { SceneryModel, SceneryReservationModel, CityModel } = require('../../models/index')

// 获取景点数据
const getSceneryData = async (req, res) => {
  try {
    let { filter_type, sort, grade, is_hot, city_id } = req.query

    // 从分页中间件中获取分页信息
    const { offset, pageSize } = req.pagination;

    // 初始化查询对象
    let query = {}
    // 初始化排序数组
    let order = [['scenery_id', 'ASC']]

    // 检查并且构建查询条件
    if (filter_type) {
      query.type = filter_type
    }

    if (grade) {
      query.grade = grade
    }

    if (is_hot) {
      query.is_hot = Number(is_hot)
    }

    if (city_id) {
      query.city_id = city_id
    }

    // 检查并且构建排序条件
    if (sort) {
      switch (sort) {
        case 'comprehensive':
          order = [['updatedAt', 'DESC'], ['createdAt', 'DESC']];
          break;
        case 'price_low_high':
          order = [['price', 'ASC']];
          break;
        case 'price_high_low':
          order = [['price', 'DESC']];
          break;
        case 'rating_high_low':
          order = [['rating', 'DESC']];
          break;
        case 'rating_low_high':
          order = [['rating', 'ASC']];
          break;
      }
    }

    // 根据查询对象查询数据库
    const sceneryData = await SceneryModel.findAndCountAll({
      where: query,
      order: order,
      attributes: ['scenery_id', 'name', 'description', 'price', 'images', 'rating', 'grade', 'type'],
      include: [
        {
          model: CityModel,
          as: 'scenery_locatedCity',
          attributes: ['name']
        }
      ],
      offset: offset,
      limit: pageSize
    })

    // 将数据转化为前端需要的格式
    const formData = sceneryData.rows.map(item => {
      return {
        id: item.scenery_id,
        name: item.name,
        description: item.description,
        price: item.price,
        image: item.images[0].url,
        rating: item.rating,
        grade: item.grade,
        city_name: item.scenery_locatedCity.name,
        type: item.type
      }
    })

    if (sceneryData.length === 0) {
      return res.status(200).json({
        title: '景点数据',
        data: [],
        message: '没有找到符合要求的景点数据'
      })
    } else {
      res.status(200).json({
        title: '景点数据',
        data: formData,
        count: sceneryData.count
      })
    }

  } catch (error) {
    res.status(500).json({
      error: error.message
    })
  }
}

// 获取热门景点数据
const getHotSceneryData = async (req, res) => {
  try {
    const hotScenery = await SceneryModel.findAll({
      where: {
        is_hot: 1
      },
      attributes: ['scenery_id', 'name', 'description', 'images'],
      limit: 5
    })

    if (hotScenery.length === 0) {
      return res.status(404).json({
        error: '未找到热门景点'
      })
    }

    // 返回前端需要的数据格式
    const formData = hotScenery.map(item => {
      return {
        id: item.scenery_id,
        name: item.name,
        description: item.description,
        image: item.images[0].url
      }
    })

    res.status(200).json({
      title: '热门景点',
      data: formData
    })
  } catch (error) {
    res.status(500).json({
      error: error.message
    })
  }
}

// 获取景点详情数据
const getSceneryDetailData = async (req, res) => {
  try {
    // 获取景点ID
    const sceneryID = req.params.id

    // 景点ID为空的情况
    if (!sceneryID) {
      return res.status(400).json({
        error: '景点ID不能为空'
      })
    }

    // 获取景点详情数据
    const sceneryDetail = await SceneryModel.findAll({
      where: {
        scenery_id: sceneryID
      },
      include: [
        {
          model: SceneryReservationModel,
          as: 'scenery_reservation',
          attributes: ['ticket_type', 'ticket_price', 'description']
        }
      ]
    })

    // 未查询到景点信息的情况
    if (!sceneryDetail.length) {
      return res.status(404).json({
        error: '未找到景点信息'
      })
    }

    res.status(200).json({
      title: '景点详情信息',
      data: sceneryDetail[0]
    })
  } catch (error) {
    res.status(500).json({
      error: error.message
    })
  }
}

module.exports = {
  getSceneryData,
  getHotSceneryData,
  getSceneryDetailData
}